//
//  classTemplateSpecialization.hpp
//  learnC++
//
//  Created by liuzhibing on 2018/6/21.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#ifndef classTemplateSpecialization_hpp
#define classTemplateSpecialization_hpp

#include <stdio.h>
#include <iostream>

template <class T>
class Storage8 {

private:
    T m_array[8]{0};
    
public:
    Storage8(){
        printf("CLASS T");
    }
    
    void set(int index,const T &value){
        m_array[index] = value;
    }
    
    const T &get(int index){
        return m_array[index];
    }
    
};
//拆分模板类定义和成员函数定义不能像普通类一样工作 - 您不能将类定义放入.cpp文件中的头和成员函数定义中。通常最好将它们全部保存在头文件中，将成员函数定义放在类的下面。

//类特化bool值
template <>

class Storage8<bool> {
private:
    unsigned char m_data;
    
public:
    Storage8():m_data(0){
        printf("BOOL");
    }
    
    void set(int index,bool value){
        unsigned char mask = 1 << index;
        if (value) {
            m_data |= mask;
        }else{
            m_data &= ~mask;  //~表示按位取反
        }
    }
    
    bool get(int index){
        unsigned char mask = 1 << index;
        return (m_data & mask) != 0;
    }
    
};

/*
 
 Storage8<int> is;
 
 for (int i = 0; i < 8; ++i) {
 is.set(i, i);
 }
 
 for (int i = 0; i < 8; ++i) {
 std::cout << is.get(i) << "\n";
 }
 
 Storage8<bool> bs;
 for (int i = 0; i < 8; ++i) {
 bs.set(i, i);
 }
 
 for (int i = 0; i < 8; ++i) {
 std::cout << bs.get(i) << "\n";
 }
 
 */


#endif /* classTemplateSpecialization_hpp */
